package com.devbobcorn.nekoration.blocks;

import java.util.Collections;
import java.util.List;

import com.devbobcorn.nekoration.blocks.entities.PrismapTableBlockEntity;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.LootContext;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;

public class PrismapTableBlock extends Block{
	protected static final VoxelShape SHAPE = Block.box(0.0D, 0.0D, 0.0D, 16.0D, 12.0D, 16.0D);
	
    public PrismapTableBlock(Properties settings) {
        super(settings);
    }

	public boolean useShapeForLightOcclusion(BlockState state) {
		return true;
	}

	public VoxelShape getShape(BlockState state, IBlockReader world, BlockPos pos, ISelectionContext ctx) {
		return SHAPE;
	}

    @Override
	public boolean hasTileEntity(BlockState state) {
		return true;
	}

	// Called when the block is placed or loaded client side to get the tile entity for the block
	// Should return a new instance of the tile entity for the block
	@Override
	public TileEntity createTileEntity(BlockState state, IBlockReader world) {
		return new PrismapTableBlockEntity();
	}

    // Called just after the player places a block.
	@Override
	public void setPlacedBy(World worldIn, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
		super.setPlacedBy(worldIn, pos, state, placer, stack);

		TileEntity tileentity = worldIn.getBlockEntity(pos);
		if (tileentity instanceof PrismapTableBlockEntity) { // prevent a crash if not the right type, or is null
            return;
		}
        LOGGER.error("Tile Entity NOT Found!");
	}

	// Press 'F5' to refresh... ^._.^==~
	public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
		if (world.isClientSide){
			TileEntity tileentity = world.getBlockEntity(pos);
			if (tileentity instanceof PrismapTableBlockEntity) { // prevent a crash if not the right type, or is null
				PrismapTableBlockEntity te = (PrismapTableBlockEntity) tileentity;
				te.createIfNull();
				te.refresh();
				LOGGER.info("Refreshing...");
			}
		}
		return ActionResultType.SUCCESS;
	}

	@Override
	public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
		return Collections.singletonList(new ItemStack(this.asItem()));
	}
}
